﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Entity;

namespace SEO.Business
{
    /// <summary>
    /// 新闻源分析
    /// </summary>
    public class BisNewsWebSEO
    {
        /// <summary>
        /// 新增新闻源
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(NewsWebSEO Info)
        {
            string strSql = "insert into [NewsWebSEO]([AdminID],[PageTitle],[PageUrl]) values(@AdminID,@PageTitle,@PageUrl)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", Info.AdminID));
            parms.Add(new SqlParameter("@PageTitle", Info.PageTitle));
            parms.Add(new SqlParameter("@PageUrl", Info.PageUrl));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 获取新闻源列表
        /// </summary>
        /// <param name="strPageTitle"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <param name="nPageIndex"></param>
        /// <param name="nPageSize"></param>
        /// <param name="nPageCount"></param>
        /// <param name="nRecordCount"></param>
        /// <returns></returns>
        public DataTable GetList(int nAdminID, string strPageTitle, DateTime dBeginTime, DateTime dEndTime, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[8];
            parms[0] = new SqlParameter("@AdminID", nAdminID);
            parms[1] = new SqlParameter("@PageTitle", strPageTitle);
            parms[2] = new SqlParameter("@BeginTime", dBeginTime);
            parms[3] = new SqlParameter("@EndTime", dEndTime);
            parms[4] = new SqlParameter("@PageIndex", nPageIndex);
            parms[5] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[6] = new SqlParameter("@PageCount", nPageCount);
            parms[6].Direction = ParameterDirection.Output;
            parms[7] = new SqlParameter("@RecordCount", nRecordCount);
            parms[7].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetNewsWebSEOList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[6].Value);
            nRecordCount = Convert.ToInt32(parms[7].Value);
            return dt;
        }

        /// <summary>
        /// 获取新闻源列表
        /// </summary>
        /// <param name="strPageTitle"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <param name="nPageIndex"></param>
        /// <param name="nPageSize"></param>
        /// <param name="nPageCount"></param>
        /// <param name="nRecordCount"></param>
        /// <returns></returns>
        public DataTable GetListAll(int nAdminID, string strPageTitle, DateTime dBeginTime, DateTime dEndTime)
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", nAdminID));
            parms.Add(new SqlParameter("@PageTitle", strPageTitle));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.Text,
                @"SELECT n.[ID],[AdminID],a.[Name],[PageTitle],[PageUrl],n.[CreateTime]
		        FROM [NewsWebSEO] n LEFT JOIN [AdminInfo] a ON n.[AdminID]=a.[ID]
		        WHERE 
		        (@AdminID=0 OR [AdminID]=@AdminID)
		        AND (@PageTitle='' OR [PageTitle] LIKE '%'+@PageTitle+'%')
		        AND n.[CreateTime] BETWEEN @BeginTime AND @EndTime",
                parms.ToArray()).Tables[0];
            return dt;
        }


        /// <summary>
        /// 验证新闻源是否存在
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="strPageUrl"></param>
        /// <returns></returns>
        public bool CheckNewsWebSEO(int nID, string strPageUrl)
        {
            object retVal = SqlHelper.ExecuteScalar(
                              SqlHelper.database,
                              CommandType.Text,
                              "select top 1 [ID] from [NewsWebSEO] where [PageUrl]=@PageUrl and (@ID=0 or [ID]<>@ID)",
                              new SqlParameter("@PageUrl", strPageUrl),
                              new SqlParameter("@ID", nID));
            return retVal == null ? false : true;
        }

        /// <summary>
        /// 删除新闻源
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [NewsWebSEO] where [ID]=@ID;",
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 删除新闻源
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll()
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [NewsWebSEO]") > 0;
        }
    }
}
